home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7250 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: gets(rec->num); I don't know what I am doing wrong...
  5. Date: 9 Feb 1996 03:36:20 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.823853493@pegasus.montclair.edu>
  8. References: <4fempt$mjg@aphex.direct.ca>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. etoivane@direct.ca (Ed Toivanen) writes:
  13.  
  14. >I posted the code that I wrote so far.  I can't make gets(rec->id); work 
  15. >properly,  I get 6 or 7 compilation errors indicating that parameter 1 does not 
  16. >match function prototype.  Each gets() call is incorrect! What to do? 
  17.  
  18.    Ok, straight out I'd say gets(rec->id) will fail because id (as defined
  19. in your code) is an integer.  gets() is typical C terse wording for "get string"
  20. so when you see gets(), think "get string."  rec->id is not a string.  The
  21. most direct solution is:
  22.  
  23. : char temp[16];  /* temporary buffer to hold string to be gotten */
  24. :
  25. : gets(temp);
  26. : rec->id = atoi(temp);  /* convert string to integer value */
  27. :                        /* no error checking is performed. */
  28.  
  29. >typedef struct science_mark {
  30. >    int math, physics, compsci; /* Range: 0..100 */
  31. >}SCIENCE_MARK;
  32.  
  33.    Notice that these values are integers.  Now if we look at your switch
  34. statement here:
  35.  
  36. >        case SCIENCE:
  37. >               printf("Math\n");
  38. >               fprintf(fp, gets(rec->marks.scientist.math));
  39.  
  40.    We see that you are using gets() to read in one of these integers.  gets()
  41. is strictly for character strings, and you'll have to implement the snippet
  42. above most of the places where gets() has generated a compilation error for
  43. you.  You'll only need one temp buffer, since the input string is discardable
  44. after you've converted it to an integer.
  45.  
  46.    Incidentally, not ALL of your gets() should have caused problems.  For
  47. example:
  48.  
  49. >    printf("Student's last name first\n");
  50. >    gets(rec->name);
  51.  
  52.    Should have compiled easily.  :)  You just need to apply the above handling
  53. for inputting integer values.
  54.                                                       -- Stone
  55. --
  56. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  57. # - Computer Science Undergrad, Montclair State University, NJ
  58. # - My views are my own, nobody else is this creative.  3;)>
  59. ... No program is so formidable that you can't just delete it.
  60.